<?php

namespace backend\controllers;

use backend\models\EduActiveOrder;
use backend\models\EduActivePay;
use backend\models\EduEnter;
use backend\models\EduEnterOrder;
use backend\models\EduEnterPay;
use backend\models\EduFeedback;
use common\models\AdminLoginForm;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use mdm\admin\models\Menu;

/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {

//        $menus = Menu::find()->asArray()->indexBy('id')->all();
//        echo "<pre>";
//        var_dump($menus);
//        echo "</pre>";
        //今日交易额 今日订单数全部有交易的表的数据和
        $active = EduActivePay::todayTrading();
        $enter = EduEnterPay::todayTrading();
        $amount = $active[0]['amount'] + $enter[0]['amount'];
        $count = $active[0]['count'] + $enter[0]['count'];

        //待付订单数量
        $activeNoPay = EduActiveOrder::nPayment();
        $enterNoPay = EduEnterOrder::nPayment();
        $nPay = $activeNoPay[0]['count'] + $enterNoPay[0]['count'];

        //待发货
        $activeStay = EduActiveOrder::stayDeliver();
        $enterStay = EduEnterOrder::stayDeliver();
        $stayDeliver = $activeStay[0]['count'] + $enterStay[0]['count'];

        //反馈
        $feedbacks = EduFeedback::find()->orderBy('create_time desc')->limit(3)->all();
//        var_dump($feedbacks);
        return $this->render('index', [
            'amount' => $amount,
            'count' => $count,
            'nPay' => $nPay,
            'stayDeliver' => $stayDeliver,
            'feedbacks' => $feedbacks,
        ]);
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new AdminLoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
}
